//
//  BTTitleImageButton.m
//  BTShike
//
//  Created by 刘军林 on 2019/5/15.
//  Copyright © 2019 刘军林. All rights reserved.
//

#import "BTTitleImageButton.h"

@interface BTTitleImageButton ()

@property (nonatomic, strong) NSString *titleString;
@property (nonatomic, strong) UIFont *titleFont;

@end

@implementation BTTitleImageButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype) initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self initInfo];
        self.titleString = self.titleLabel.text;
        self.titleFont = self.titleLabel.font;
    }
    return self;
}

-(instancetype) init
{
    if (self = [super init]) {
        [self initInfo];

    }
    return self;
}

-(instancetype) initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self initInfo];
    }
    return self;
}
         
-(void) initInfo
{
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.titleLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
    [self.titleLabel addObserver:self forKeyPath:@"font" options:NSKeyValueObservingOptionNew context:nil];
}

-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"text"]) {
        self.titleString = change[@"new"];
        CGRect rect = self.frame;
        
        CGSize size = [self.titleString sizeWithAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:16.f]}];
        rect.size.width = size.width + 20;
        self.frame = rect;
        
    }else if([keyPath isEqualToString:@"font"]) {
        self.titleFont = change[@"new"];
    }
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect {
    CGFloat width = CGRectGetWidth(contentRect);

    CGFloat inteval = CGRectGetHeight(contentRect)/8.0;
    //设置图片的宽高为button高度的3/4;
    CGFloat imageH = CGRectGetHeight(contentRect) - 2 * inteval;
    
    CGSize size = [self.titleString sizeWithAttributes:@{NSFontAttributeName :self.titleFont}];

    CGFloat titleImageWidth = size.width + inteval*2 + 10; //文字宽度，文字与画图片间隔，w图片宽度

    if (titleImageWidth + inteval * 2 > width) {
        titleImageWidth = width - inteval * 2;
        size.width = titleImageWidth - inteval * 2 - 10;
    }

    CGRect rect = CGRectMake((width - titleImageWidth) / 2 + size.width + inteval + inteval, (contentRect.size.height - 15) / 2, 15, 15);
    return rect;
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect {
    
    CGFloat width = CGRectGetWidth(contentRect);
    CGFloat inteval = CGRectGetHeight(contentRect)/8.0;
    //设置图片的宽高为button高度的3/4;
    CGFloat imageH = CGRectGetHeight(contentRect) - 2 * inteval;
    
    CGSize size = [self.titleString sizeWithAttributes:@{NSFontAttributeName : self.titleFont}];
    CGFloat titleImageWidth = size.width + inteval*2 + 10; //文字宽度，文字与画图片间隔，w图片宽度

    if (titleImageWidth + inteval * 2 > width) {
        titleImageWidth = width - inteval * 2;
        size.width = titleImageWidth - inteval * 2 - 10;
    }
    
    CGRect rect = CGRectMake((width - titleImageWidth) / 2, (CGRectGetHeight(contentRect) - size.height) / 2, CGRectGetWidth(contentRect) - imageH - 2*inteval + 10, CGRectGetHeight(contentRect) - 2*inteval);
    
    return rect;
}

-(UIFont *) titleFont
{
    if (!_titleFont) {
        _titleFont = [UIFont boldSystemFontOfSize:16.f];
    }
    return _titleFont;
}

-(NSString *) titleString
{
    if (!_titleString) {
        _titleString = @"111111111";
    }
    return _titleString;
}

@end
